<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Player;

class Player
{
    protected $identity;

    protected $roomId;

    protected $placeNumber;

    private $id;

    private $speakTimer;

    private $gameRole; // 村民 狼人 女巫...

    public function __construct($id, $identity, $roomId, $placeNumber = 0)
    {
        $this->identity = $identity;
        $this->id = $id;
        $this->roomId = $roomId;
        $this->placeNumber = $placeNumber;
    }

    public function speak()
    {
        $flag = 0;
        $timerId = swoole_timer_tick(1000, function ($timerId) use (&$flag) {
            if ($flag >= 30) {
                $this->speakTimer = null;
                swoole_timer_clear($timerId);
            }
            ++$flag;
            // Send Message...
        });
        $this->speakTimer = $timerId;
        return $this->speakTimer;
    }

    public function speakOver()
    {
        // 之前清理过了 道理上这个不会被触发 因为只有在提前结束发言才会调用此方法
        if (! $this->speakTimer) {
            return;
        }
        swoole_timer_clear($this->speakTimer);
        $this->speakTimer = null;
    }

    public function setPlaceNum($placeNum)
    {
        $this->placeNumber = $placeNum;
    }

    public function getPlaceNum()
    {
        return $this->placeNumber;
    }

    public function getSpeakTimerId()
    {
        return $this->speakTimer;
    }

    // 被杀
    public function killByWerewolf()
    {
        $this->beGhost();
    }

    public function killByWitch()
    {
        $this->beGhost();
    }

    public function killByHunte()
    {
        $this->beGhost();
    }

    public function getGameRole()
    {
        return $this->gameRole;
    }

    public function setGameRole($role)
    {
    }

    public function beGhost()
    {
        $this->identity = 'ghost';
    }
}
